package com.book.util;

import cn.hutool.core.io.FileUtil;
import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.exception.InvalidConfigurationException;
import org.mybatis.generator.exception.XMLParserException;
import org.mybatis.generator.internal.DefaultShellCallback;

import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author SuperYu  2024/6/14
 */
public class Generator {
    // 1：备份
    public static void backUp(){
        String mapperLocation = "book-mall-hz11-mapper/src/main/java/com/book/mapper";
        File mapperFolder = new File(mapperLocation);

        File[] files = mapperFolder.listFiles();
        if(files != null && files.length != 0){
            // 备份的文件夹
            File backupFolder = new File("book-mall-hz11-mapper/src/main/java/com/book/backup");
            // 使用hutool工具进行文件复制操作
            FileUtil.copyContent(mapperFolder,backupFolder,false);
            System.out.println("---backup ok ---");
        }
    }


    // 2：逆向工程生成我们的代码：---------------------------
    public static void create() {
        List<String> warnings = new ArrayList<String>();
        boolean overwrite = true;
        ConfigurationParser cp = new ConfigurationParser(warnings);
        Configuration config = null;

        try {
            config = cp.parseConfiguration(
                    // Generator.class.getResourceAsStream("generatorConfig.xml"));
                    Generator.class.getClassLoader().getResourceAsStream("generatorConfig.xml"));

            DefaultShellCallback callback = new DefaultShellCallback(overwrite);
            MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
            myBatisGenerator.generate(null);

        } catch (IOException e) {
            e.printStackTrace();
        } catch (XMLParserException e) {
            e.printStackTrace();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (InvalidConfigurationException e) {
            e.printStackTrace();
        }
    }

    // 3: 恢复原来写好的代码：
    public static void restore(){
        String backupLocation = "book-mall-hz11-mapper/src/main/java/com/book/backup";
        File backupFolder = new File(backupLocation);
        // 得到备份目录的所有内容
        File[] files = backupFolder.listFiles();
        if(files != null && files.length != 0){
            // 还原数据
            String mapperLocation = "book-mall-hz11-mapper/src/main/java/com/book/mapper";
            File mapperFolder = new File(mapperLocation);
            // 使用工具进行复制，注意这里要是覆盖写入
            FileUtil.copyContent(backupFolder,mapperFolder, true);
            // 最后要把备份文件夹删除
            boolean f = FileUtil.del(backupFolder);
            System.out.println("--- 备份文件夹是否删除："+f);
        }
    }


    public static void main(String[] args) {
        // 1：先备份代码
        backUp();
        // 2：生成新代码
        create();
        // 3：把原来的代码复制回去
        restore();

        System.out.println("----end----");
    }
}
